from sqlalchemy.orm import relationship, backref

from database_connection.index import Base, engine
from sqlalchemy import Text, Column, Integer


class Blog(Base):  # 必须继承declaraive_base得到的那个基类
    __tablename__ = "Blog"  # 必须要有__tablename__来指出这个类对应什么表，这个表可以暂时在库中不存在，SQLAlchemy会帮我们创建这个表
    id = Column(Integer, nullable=False, primary_key=True, autoincrement=True, unique=True, comment="id")
    label = Column(Text, nullable=False)  # 标题
    abstract = Column(Text, nullable=False)  # 摘要
    blog = Column(Text(length=4294967295), nullable=False)  # 博客
    storage_time = Column(Text, nullable=False)  # 存储时间
    like_number = Column(Integer, nullable=False, default=0)  # 点赞次数
    year = Column(Integer, nullable=False)  # 年
    month = Column(Integer, nullable=False)  # 月
    day = Column(Integer, nullable=False)  # 日

    blog_class_list = relationship("Class", secondary="Class_Link_Blog", back_populates="class_blog_list")
    blog_admin_list = relationship("Admin", secondary="Admin_Link_Blog", back_populates="admin_blog_list")

    def blog_to_dict(self):
        return {
            'id': self.id,
            'label': self.label,
            'abstract': self.abstract,
            'blog': self.blog,
            'storage_time': self.storage_time,
            'like_number': self.like_number,
            'year': self.year,
            'month': self.month,
            'day': self.day
        }

    def __init__(self, label, abstract, blog, storage_time, like_number, year, month, day):
        self.label = label
        self.abstract = abstract
        self.blog = blog
        self.storage_time = storage_time
        self.like_number = like_number
        self.year = year
        self.month = month
        self.day = day


Base.metadata.create_all(engine)
